package TestPaperSystem.service.impl;

import TestPaperSystem.Dto.PageUtils_VO;
import TestPaperSystem.Dto.UserExamDTO;
import TestPaperSystem.entity.UserExam;
import TestPaperSystem.mapper.UserExamMapper;
import TestPaperSystem.service.UserExamService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwj
 * @since 2023-03-25
 */
@Service
public class UserExamServiceImpl extends ServiceImpl<UserExamMapper, UserExam> implements UserExamService {

    @Resource
    UserExamMapper userExamMapper;

    @Override
    public UserExam checkByUserId(String userId) throws Exception{
        QueryWrapper<UserExam> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserExam::getUserId,userId);
        UserExam userExam = userExamMapper.selectOne(queryWrapper);
        return userExam;
    }

    @Override
    public Page<UserExamDTO> getMyScoreByUser(PageUtils_VO page) throws Exception{
        Page<UserExamDTO> dtoPage = new Page<>();

        long counts = userExamMapper.selectCount(new QueryWrapper<UserExam>().lambda().eq(UserExam::getUserId,page.getId()));

        List<UserExamDTO> selfTestScore = userExamMapper.getSelfTestScore(page.getId() ,
                (page.getCurrentPage() - 1) * page.getSize() , page.getSize());
        dtoPage.setCurrent(page.getCurrentPage());
        dtoPage.setRecords(selfTestScore);
        dtoPage.setTotal(counts);
        return dtoPage;
    }
}
